package com.aierjun.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.web.servlet.ModelAndView;

import com.aierjun.bean.UserBean;
import com.aierjun.utils.ConnectionUtils;

public class UserDao {
	public static final String FIND_ALL_EMP = "insert into userInfo values(?,?,?) ";
	public void saveSQL(UserBean userBean){
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = ConnectionUtils.getConnection();
			stmt = conn.prepareStatement(FIND_ALL_EMP);
	        stmt.setObject(1, userBean.getUname());
	        stmt.setObject(2,  userBean.getPassword());
	        stmt.setObject(3,  userBean.getToken());
	        int  rs = stmt.executeUpdate();
	        System.out.println(rs+"");
	        if (rs!=0) {
				System.out.println("OK");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        
	
	}
	
	
	
	public static final String FIND_ALL_EMP_QUERY = "select * from userInfo where cast([token] as nvarchar(max)) = ";//
	public void querySQL(UserBean userBean){
		Connection conn = null;
		Statement stmt = null;
		ConnectionUtils.closeConnection();
		try {
			conn = ConnectionUtils.getConnection();
			stmt = conn.createStatement(); 
//			stmt.setString(1,"1519314063332");
			ResultSet  resultSet = stmt.executeQuery(FIND_ALL_EMP_QUERY + userBean.getToken());
			while(resultSet.next()){
				String one = resultSet.getString(1);
				String two = resultSet.getString(2);
				String three = resultSet.getString(3);
				System.out.print(one + "..." + two + "..." + three);
			}
	      
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        
	
	}
}
